<?php
/**
 * 投票
 */
namespace app\admin\controller;
class Vote extends base
{
    //前置方法
    protected $beforeActionList = [
        'map'  =>  ['only'=>'index'],
    ];

    /**
     * 搜索
     */
    protected function map(){
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }



    public function edit($id)
    {
        if (intval($id) < 1) {
            $this->error('数据错误');
        }
        //清理缓存
        cache($this->request->controller().$id,null);
        $data = $this->model->get($id);
        $data['statdate'] = date("Y-m-d H:i:s", $data['statdate']);
        $data['enddate'] = date("Y-m-d H:i:s", $data['enddate']);
        $items = db('vote_item')->where(array('vid' => $id))->order('id asc')->select();
        $this->assign('items', $items);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function count($id)
    {
        $record = db('vote_record')->where(array('vid' => $id))->order('id asc')->select();

        $data = db('Vote_item')->field('`item` as `name`,`vcount` as `value`')->where('vid',$id)->select();
        $echarts = json_encode($data);

        foreach($record as $key=>$value){
            $record[$key]['itemname']   = $this->_getItemName($value['item_id']);
        }

        $this->assign('record',$record);
        $this->assign('echarts',$echarts);
        return $this->fetch();
    }

    /**
     * @param $item_id
     * @return string获取投票名字
     */
    public function _getItemName($item_id){
        $id     = explode(',', $item_id);
        $name   = '';
        foreach ($id as $key => $value) {
            $name .= db('Vote_item')->where('id',$value)->value('item').',';
        }
        return rtrim($name,',');
    }

    /**
     * 保存
     */
    public function save()
    {

        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $item_add =array();
        $datas = $this->request->param()['add'];

        foreach ($datas as $ke => $value) {
            foreach ($value as $k => $v) {
                if( $v != ""){
                    $item_add[$k][$ke]=$v;
                }
            }
        }
        if(empty($item_add)){
            return ajax_info('投票选项必须填写');
            exit();
        }
        $data = $this->request->param();
        $t_item = db('Vote_item');
        $data['statdate'] = strtotime($data['statdate']);
        $data['enddate'] = strtotime($data['enddate']);
        if(!empty($data['id'])){
            $this->model->allowField(true)->save($data, ['id' =>$data['id']]);
            $t_item->where('vid',$data['id'])->find();
            foreach ($item_add as $k => $v) {
                if(!empty($v['item'])){
                    if(!empty($v['id'])){
                        $i_id['id']=$v['id'];
                        $data2['item']=$v['item'];
                        $data2['rank']=empty($v['rank']) ? "1" : $v['rank'];
                        $data2['vcount']=empty($v['vcount']) ? "0" : $v['vcount'];
                        $t_item->where(array('id'=>$i_id['id'],'vid'=>$data['id']))->update($data2);
                    }else{
                        $data2['vid'] = $data['id'];
                        $data2['item']=$v['item'];
                        $data2['rank']=empty($v['rank']) ? "1" : $v['rank'];
                        $data2['vcount']=empty($v['vcount']) ? "0" : $v['vcount'];
                        $t_item->insert($data2);
                    }
                }
            }
            return ajax_info('修改成功！' ,'u');
        }

        $data['lang']=cookie('think_var_admin');
        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            foreach ($item_add as $k => $v) {
                if(!empty($v['item'])){
                    $data2['vid'] = $this->model->id;
                    $data2['item']=$v['item'];
                    $data2['rank']=empty($v['rank']) ? "1" : $v['rank'];
                    $data2['vcount']=empty($v['vcount']) ? "0" : $v['vcount'];
                    $t_item->insert($data2);
                }
            }
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }

    public function del_tab($id){

        db('Vote_item')->where(array('id'=>$id))->delete();
        //$arr=array('errno'=>0,'tid'=>$da['tid']);
        //echo json_encode($arr);
        exit;
    }
}
